package niuke;

/**
 * description:
 * author:张腾
 * date:2021-06-28
 */

/**
 * 给定一个单链表，请设定一个函数，将链表的奇数位节点和偶数位节点分别放在一起，重排后输出。
 * 注意是节点的编号而非节点的数值
 */
public class NC133 {
    public ListNode oddEvenList (ListNode head) {
        // write code here
        if (head==null || head.next==null) return head;
        ListNode SHead = head;
        ListNode HHead = head.next;
        ListNode s = head, h = HHead;
        ListNode sPre = s, hPre = h;
        while (s.next!=null && h.next!=null){
            s = s.next.next;
            h = h.next.next;
            sPre.next = s;
            hPre.next = h;
            sPre = s;
            hPre = h;
        }
        sPre.next = HHead;
        return head;
    }
}
